package com.example.wan_android.ui.fragment

import android.util.Log
import android.view.View
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.wan_android.BR
import com.example.wan_android.R
import com.example.wan_android.adapter.AuthorContentAdapter
import com.example.wan_android.adapter.AuthorTabAdapter
import com.example.wan_android.databinding.FragmentAuthorBinding
import com.example.wan_android.tools.SpacesItemDecoration
import com.example.wan_android.tools.SpacesItemEndDecoration
import com.example.wan_android.viewmodel.AuthorViewModel

class AuthorFragment : BaseFragment<FragmentAuthorBinding, AuthorViewModel>(
    AuthorViewModel::class.java,
    R.layout.fragment_author,
    BR.viewModel
) {

    private val authorNameAdapter by lazy {
        AuthorTabAdapter(mViewModel!!)
    }

    private val authorContentAdapter by lazy {
        AuthorContentAdapter(mViewModel!!)
    }

    override fun initViews() {
        mViewDataBinding?.rvAuthorTab?.adapter = authorNameAdapter
        mViewDataBinding?.rvAuthorTab?.layoutManager =
            LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false)
        mViewDataBinding?.rvAuthorTab?.addItemDecoration(SpacesItemEndDecoration(15))
        mViewModel?.tabLiveData?.observe(this) {
            it.onSuccess {
                authorNameAdapter.setListData(it)
            }
            it.onFailure {
                Log.i("TAG", "initViews: " + it)
            }
        }
        mViewModel?.isSuccess?.observe(this) {
            if (it) {
                mViewDataBinding?.ivEmpty?.visibility = View.GONE
                mViewDataBinding?.rvAuthorItem?.visibility = View.VISIBLE
            } else {
                mViewDataBinding?.ivEmpty?.visibility = View.VISIBLE
                mViewDataBinding?.rvAuthorItem?.visibility = View.GONE
            }
        }
        mViewDataBinding?.rvAuthorItem?.adapter = authorContentAdapter
        mViewDataBinding?.nvNestedScroll?.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            if (isNearBottom(scrollY)) {
                mViewModel?.getAuthorArticleInfoData()
            }
        })
        mViewModel?.isClear?.observe(this) {
            if (it) {
                authorContentAdapter.clear()
                mViewModel?.setClear(false)
            }
        }
        mViewDataBinding?.rvAuthorItem?.layoutManager =
            LinearLayoutManager(this.context)
        mViewDataBinding?.rvAuthorItem?.addItemDecoration(SpacesItemDecoration(30))
        mViewModel?.contentLiveData?.observe(this) {
            it.onSuccess {
                authorContentAdapter.addListData(it)
            }
            it.onFailure {
                Log.i("TAG", "initViews: " + it)
            }
        }

    }

    // 判断是否接近底部
    private fun isNearBottom(scrollY: Int): Boolean {
        val contentHeight = mViewDataBinding?.nvNestedScroll?.getChildAt(0)?.height
        val viewHeight = mViewDataBinding?.nvNestedScroll?.height

        return scrollY >= (contentHeight!! - viewHeight!!)
    }
}